<template>
  <PanelHead :route="route"></PanelHead>
  <el-button type="primary" icon="Plus" @click="open(null)" size="small">新增</el-button>
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id" width="250" />
    <el-table-column prop="name" label="昵称" width="250" />
    <el-table-column prop="permissionName" label="菜单权限" width="480" />
    <el-table-column label="操作">
      <!-- #default 表示这是默认插槽。   "scope" 是一个对象，它包含了子组件传递给插槽的数据和方法 -->
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="pagination-info">
    <el-pagination v-model:current-page="paginationData.pageNum" :page-size="paginationData.pageSize"
      :background="false" size="small" layout="total, prev, pager, next" :total="tableData.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <!-- 弹框 -->
  <el-dialog v-model="dialogTableVisible" title="添加权限" width="500" :before-close="beforeClose">
    <el-form ref="formRef" :rules="rules" :model="form" label-width="100px" label-position="left">
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请填写权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="permissions">
        <!-- :default-checked-keys="[3,4]" 是默认选中的  但是应该是[4,5]才对 -->
        <!-- 在模板中使用 ref 属性时，它用于给元素或子组件注册引用信息 用来访问 DOM 元素或子组件实例 -->
        <!--  -->
        <el-tree ref="treeRef" style="max-width:600px;" :data="permissionData" show-checkbox node-key="id"
          :default-checked-keys="defaultKeys" :default-expanded-keys="[2]" />
      </el-form-item>
    </el-form>
    <!-- 添加一个按钮插槽 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>

  </el-dialog>
</template>


<script setup>
  import { ref, reactive, onMounted, nextTick } from 'vue'
  import { userGetMenu, userSetMenu, menuList } from '../../../api/login.js'
  import { useRoute } from 'vue-router';
  const route = useRoute()


  const dialogTableVisible = ref(false)//控制弹框的状态  显示和隐藏
  const formRef = ref()
  const defaultKeys = [4, 5] //选中权限的id
  const form = reactive({
    id: '',
    name: '',//选中的名称
    permissions: '' //选中的权限
  })
  const permissionData = ref([])//树形菜单权限的数据
  const treeRef = ref()
  //菜单列表数据
  const tableData = reactive({
    list: [],
    total: 0
  })

  // 表单校验规则
  const rules = reactive({
    name: [{ required: true, trigger: 'blur', message: '请输入权限名称' }]
  })

  const paginationData = reactive({
    pageNum: 1,
    pageSize: 10
  })

  onMounted(() => {
    // 获取菜单权限数据
    userGetMenu().then(({ data }) => {
      // console.log(data);
      permissionData.value = data.data
    })
    getListData()
  })

  // 请求列表数据
  const getListData = () => {
    menuList(paginationData).then(({ data }) => {
      const { list, total } = data.data
      tableData.list = list
      tableData.total = total
    })
  }

  // 打开弹窗
  const open = (rowData = {}) => {
    dialogTableVisible.value = true//弹框打开
    // 此时是编辑状态  弹窗打开后，form是异步生成的，需要填充到弹窗中
    nextTick(() => {
      if (rowData) {
        Object.assign(form, { id: rowData.id, name: rowData.name })
        treeRef.value.setCheckedKeys(rowData.permission)
      }
    })

  }

  // 点击页码所对应的函数
  const handleSizeChange = (val) => {
    paginationData.pageSize = val
    getListData()
  }
  // 点击当前页所对应的函数
  const handleCurrentChange = (val) => {
    paginationData.pageNum = val
    getListData()
  }

  // 关闭前的回调，会暂停 Dialog 的关闭.
  const beforeClose = () => {
    dialogTableVisible.value = false
    formRef.value.resetFields() //重置表单
    treeRef.value.setCheckedKeys(defaultKeys)
  }

  // 表单提交  表单校验
  const confirm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
      if (valid) {
        // 菜单权限修改
        // 获取选择的checkbox数据
        const permissions = JSON.stringify(treeRef.value.getCheckedKeys())
        // console.log(treeRef);
        // console.log(permissions);
        userSetMenu({ name: form.name, permissions, id: form.id }).then(({ data }) => {
          // console.log(data);
          beforeClose()//关闭弹窗
          getListData()//重新获取数据  刷新页面


        })
      } else {
        console.log('error submit!', fields)

      }
    })
  }



</script>

<style lang="less" scoped></style>